{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}
<script>
$(function() {
    var id = parseInt('{{ id }}');
    if (id > 0) {
        getJSON('/api/singlePages/' + id, function (err, singlePage) {
            if (err) {
                return showError(err);
            }
            singlePage.publishAtLocal = timestampToDateTimeLocal(singlePage.publishAt);
            initVM({
            	title: 'Edit Single Page',
            	singlePage: singlePage
            });
        });
    }
    else {
        initVM({
        	title: 'New Single Page',
        	singlePage: {
        		publishAtLocal: timestampToDateTimeLocal(Date.now())
        	}
        });
    }
});

function initVM(data) {
    window.vm = new Vue({
        el: '#vm',
        data: {
        	title: data.title,
            singlePage: data.singlePage
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            submit: function() {
            	this.singlePage.publishAt = dateTimeLocalToTimestamp(this.singlePage.publishAtLocal);
                this.singlePage.content = $('#content-editor').val();
                var that = this;
                this.$resource('{{ action }}').save(this.singlePage).then(function (resp) {
                    resp.json().then(function (result) {
                        that.saved = true;
                        location.assign('/manage/single/');
                    });
                }, onJsonFormError);
            }
        }
    });

    // check content when unload:
    window.onbeforeunload = function() {
        if (window.vm.saved) {
            return;
        }
        if (window.vm.singlePage.content !== $('#content-editor').val()) {
            return 'Content has been changed.';
        }
    };

    $('#content-editor').val(window.vm.singlePage.content || '');
    var htmleditor = UIkit.htmleditor($('#content-editor').get(0), {
        markdown: true,
        maxsplitsize: 600
    });
    // override image action:
    htmleditor.off('action.image');
    htmleditor.on('action.image', function() {
        uploadImage(function (err, result) {
            if (err) {
                return;
            }
            htmleditor['replaceSelection']('\n![' + result.name + '](' + result.url + ')\n');
        });
    });
    window.htmleditor = htmleditor;
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <form v-on:submit.prevent="submit" class="uk-form uk-form-stacked uk-margin">
            <legend v-text="title"></legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>

                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Name') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="singlePage.name" name="name" type="text" maxlength="100" class="uk-width-1-1" placeholder="name">
                    </div>
                </div>

                <div class="uk-form-row">
                    <label class="uk-form-label">Tags:</label>
                    <div class="controls">
                        <input v-model="singlePage.tags" name="tags" type="text" class="uk-width-1-1" placeholder="tag1, tag2, tag3">
                    </div>
                </div>

                <div class="uk-form-row">
                    <label class="uk-form-label">Publish at:</label>
                    <div class="controls">
                        <label class="checkbox">
                            <input v-model="singlePage.publishAtLocal" name="publishAtLocal" type="datetime-local">
                        </label>
                    </div>
                </div>

                <div class="uk-form-row">
                    <label class="uk-form-label">Content:</label>
                    <div class="controls">
                        <textarea id="content-editor" rows="20" style="width:100%; resize:none;"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> {{ _('Save') }}</button>
                        <button type="button" class="uk-button" onclick="location.assign('{{ form.redirect }}')"><i class="uk-icon-times"></i> {{ _('Cancel') }}</button>
                    </div>
                </div>
        </fieldset>
    </form>
</div>

{% endblock %}
